package com.ycj.pojo;

import com.ycj.pojo.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class mybatisDemo2 {
    public static void main(String[] args) throws IOException {
        //1.加载mybatis的配置文件，获取sqlsessionfactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =  new SqlSessionFactoryBuilder().build(inputStream);

        //2.获取sql对象,获取之后就可以执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

//        //3.执行sql
//        List<User> user = sqlSession.selectList("test.selectAll");
//        System.out.println(user);
        //3.1获取Usermapper的代理对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.selectAll();
        System.out.println(users);

        //4.释放资源
        sqlSession.close();
    }
}
